var car_series_id = 0; //车系id
var car_series_car_level_fk_id = 0; //车系级别id
var whether_full_screen = false; //是否已全屏

$(document).ready(function() {
	get_user_mess_and_token();
	var url = location.search;
	car_series_id = url.substr(1); //车系id
	/* 修改登录url */
	$(".to_login").attr("href", "http://127.0.0.1:8899/html/car_login.html?car_series_index/" +
		car_series_id);
	if (token != "") { // 已登录
		$(".follow_empty__2-g7o").hide();
	}
	$(".rank_tags__Edels span:eq(0)").text(new Date().getMonth() + "月销量");
	load_all_data();
});

/* 加载全部数据 */
function load_all_data() {
	/* 加载车系信息 */
	$.post("http://127.0.0.1:8899/api/consumer/PublicController/load_all_car_series_index_data/" + car_series_id,
		function(data) {
			/* 加载所有信息 */
			var car_series = data.car_series;
			car_series_car_level_fk_id = car_series.car_series_car_level_fk_id; /* 车系级别id */
			var brand_id = car_series.brand.brand_id; /* 品牌id */
			$("#brand_name").text(car_series.brand.brand_name); //品牌名称
			$("#car_series_name").text(car_series.car_series_name); //车系名称
			$(".header-left_series-name__3Kfeh").text(car_series.car_series_name);
			$(".header-left_logo__3_20J").attr("src", "http://120.48.47.144:8899/images/" + car_series.brand
				.brand_images.brand_images_car_logo_url); /* 车系logo */
			$(".header-left_agent-price__qvpXV").text(car_series.min_distributer_price + "-" + car_series
				.max_distributer_price + "万"); /* 经销商报价 */
			$(".header-left_facture-price__bMBc3").text(car_series.min_manufacturer_price + "-" + car_series
				.max_manufacturer_price + "万"); /* 厂商指导价 */
			$("#car_series_images_url_1").attr("src", "http://120.48.47.144:8899/images/" + car_series
				.car_series_images_url_1); /* 车系图片1 */
			$("#car_series_long").text(car_series.car_series_long + "mm"); /* 长 */
			$("#car_series_wide").text(car_series.car_series_wide + "mm"); /* 宽 */
			$("#car_series_high").text(car_series.car_series_high + "mm"); /* 高 */
			$("#car_series_wheelbase").text(car_series.car_series_wheelbase + "mm"); /* 轴距 */
			$("#car_model_front_suspension_type").text(car_series.car_model_front_suspension_type); /* 前悬架 */
			$("#car_model_rear_suspension_type").text(car_series.car_model_rear_suspension_type); /* 前悬架 */
			$("#default_car_model").text(car_series.car_model.car_model_name); /* 默认查询一辆车型名称 */
			$("#appearance_images_count").text(car_series.appearance_images_count + "张"); /* 外观数量 */
			$("#interior_images_count").text(car_series.interior_images_count + "张"); /* 内饰数量 */
			$("#space_images_count").text(car_series.space_images_count + "张"); /* 空间数量 */
			$("#auto_show_images_count").text(car_series.auto_show_images_count + "张"); /* 车展数量 */
			$("#car_series_ul").text(car_series.car_series_name + "·车型列表");
			$("#car_actual_large_picture_all_type a:eq(0)").attr("href", //外观url
				"http://127.0.0.1:8899/html/car_actual_large_picture.html?" + car_series.brand.brand_id + "/" +
				car_series_id + "/0/1"); 
			$("#car_actual_large_picture_all_type a:eq(1)").attr("href", //内饰url
				"http://127.0.0.1:8899/html/car_actual_large_picture.html?" + car_series.brand.brand_id + "/" +
				car_series_id + "/0/2");
			$("#car_actual_large_picture_all_type a:eq(2)").attr("href", //空间url
				"http://127.0.0.1:8899/html/car_actual_large_picture.html?" + car_series.brand.brand_id + "/" +
				car_series_id + "/0/3");
			$("#car_actual_large_picture_all_type a:eq(3)").attr("href", //车展url 
				"http://127.0.0.1:8899/html/car_actual_large_picture.html?" + car_series.brand.brand_id + "/" +
				car_series_id + "/0/4"); 
			$("#to_car_vehicle_selection").attr("href",
			"http://127.0.0.1:8899/html/car_vehicle_selection.html"); //当前位置--选车
			$("#brand_name").attr("href", "http://127.0.0.1:8899/html/car_vehicle_selection.html?" + car_series
				.brand.brand_id +
				"," + car_series.brand.brand_name +
				"-x-x-x-x-x-x");
			$("#anchor-nav a:eq(0)").attr("href", "http://127.0.0.1:8899/html/car_actual_large_picture.html?" +
				car_series.brand.brand_id + "/" + car_series_id + "/0/0"
			); //实拍大图
			$("#anchor-nav a:eq(1)").attr("href", "http://127.0.0.1:8899/html/car_series_article.html?" +
				car_series_id); //文章
			$("#anchor-nav a:eq(2)").attr("href", "http://127.0.0.1:8899/html/car_series_video.html?" +
				car_series_id); //视频
			var all_car_model_data = "";
			/* 加载所有车型 */
			$(data.all_car_model).each(function(index, item) {
				all_car_model_data += "<li> " +
					"<div class=\"jsx-3056992443 tw-grid table\">" +
					"<div class=\"jsx-3056992443 table_thead__3SCmw table_th__YAY7N\">" +
					"<span style=\"display: inline-block;width:388px;margin-left: 20px;\">" + item
					.car_model_engine + " / " + item.energy.energy_name + "</span>" +
					"<span style=\"display: inline-block;width:280px\">指导价</span>" +
					"<span style=\"display: inline-block;width:280px\">经销商报价</span>" +
					"</div>" +
					"</div>" +
					"<div " +
					"class=\"jsx-3056992443 tw-grid table table_body__2HlCD table_clickable__1dD3_\"> " +
					"<div " +
					"class=\"jsx-3056992443 table_normal__LUrBM table_td__3aYdO table_no-follow__25ewE\"> " +
					"<div style=\"display: inline-block;width: 388px;margin-left: 20px;\"> " +
					"<div " +
					"class=\"jsx-1226022265 line-2 tw-text-16 tw-flex tw-items-center\"> " +
					"<a href=\"http://127.0.0.1:8899/html/car_model_index.html?" + car_series.brand
					.brand_id + "/" + car_series_id + "/" + item.car_model_id + "\" target=\"_blank\" " +
					"rel=\"opener\">" + item.car_model_name + "</a> " +
					"</div> " +
					"<div class=\"tw-mt-6 tw-leading-26\"><button type=\"button\" " +
					"class=\"tw-w-70 tw-h-28 tw-rounded-2  tw-border-solid tw-align-top tw-mr-4 tw-cursor-default tw-border-color-gray-500 tw-text-common-blue hover:tw-bg-common-yellow hover:tw-bg-opacity-12 hover:tw-border-common-yellow hover:tw-text-common-black tw-border-1\" " +
					"data-log-click=\"{&quot;event&quot;:&quot;clk_event&quot;,&quot;params&quot;:{&quot;event_id&quot;:&quot;car_style_module_config_btn&quot;,&quot;module_id&quot;:&quot;car_style_module&quot;,&quot;car_series_id&quot;:&quot;1145&quot;,&quot;car_series_name&quot;:&quot;轩逸&quot;}}\"><a " +
					"target=\"_blank\" href=\"http://127.0.0.1:8899/html/car_series_video.html?" +
					car_series_id +
					"\" " +
					"rel=\"opener\"><i " +
					"class=\"DCD_Icon icon_setting_24 tw-mr-2 tw-text-14\"></i>视频</a></button> " +
					"<button type=\"button\" " +
					"class=\"tw-w-70 tw-h-28 tw-rounded-2  tw-border-solid tw-align-top tw-mr-4 tw-cursor-default tw-border-color-gray-500 tw-text-common-blue hover:tw-bg-common-yellow hover:tw-bg-opacity-12 hover:tw-border-common-yellow hover:tw-text-common-black tw-border-1\" " +
					"data-log-click=\"{&quot;event&quot;:&quot;clk_event&quot;,&quot;params&quot;:{&quot;event_id&quot;:&quot;car_style_module_picture_btn&quot;,&quot;module_id&quot;:&quot;car_style_module&quot;,&quot;car_series_id&quot;:&quot;1145&quot;,&quot;car_series_name&quot;:&quot;轩逸&quot;}}\"><a " +
					"target=\"_blank\" " +
					"href=\"http://127.0.0.1:8899/html/car_actual_large_picture.html?" + car_series.brand
					.brand_id + "/" + car_series_id + "/" + item.car_model_id + "/0" + "\" " +
					"rel=\"opener\"><i " +
					"class=\"DCD_Icon icon_image_24 tw-mr-2 tw-text-14\"></i>图片</a></button> <button type=\"button\" " +
					"class=\"tw-w-70 tw-h-28 tw-rounded-2  tw-border-solid tw-align-top tw-mr-4 tw-cursor-default tw-border-color-gray-500 tw-text-common-blue hover:tw-bg-common-yellow hover:tw-bg-opacity-12 hover:tw-border-common-yellow hover:tw-text-common-black tw-border-1\" " +
					"data-log-click=\"{&quot;event&quot;:&quot;clk_event&quot;,&quot;params&quot;:{&quot;event_id&quot;:&quot;car_style_module_picture_btn&quot;,&quot;module_id&quot;:&quot;car_style_module&quot;,&quot;car_series_id&quot;:&quot;1145&quot;,&quot;car_series_name&quot;:&quot;轩逸&quot;}}\"><a " +
					"target=\"_blank\" " +
					"href=\"http://127.0.0.1:8899/html/car_series_article.html?" + car_series_id + "\" " +
					"rel=\"opener\"><i " +
					"class=\"DCD_Icon icon_calculate_24 tw-mr-2 tw-align-bottom\"></i>文章</a></button>" +
					"</div> " +
					"</div> " +
					"<div style=\"display: inline-block;width: 280px;\">" + item
					.car_model_manufacturer_price + "万</div> " +
					"<div style=\"display: inline-block;width: 280px;color: red;\">" + item
					.car_model_distributer_price + "万</div> " +
					"<div style=\"display: inline-block;width: 170px;\"> " +
					"<div class=\"tw-flex tw-items-center tw-flex-nowrap\"> " +
					"<button type=\"button\" " +
					"class=\"tw-rounded-2 tw--mt-4 tw-align-top tw-text-12  tw-bg-common-yellow hover:tw-bg-common-highlight-yellow\" " +
					"data-log-click=\"{&quot;event&quot;:&quot;clk_event&quot;,&quot;params&quot;:{&quot;event_id&quot;:&quot;car_style_module_dealer_quoted_price&quot;,&quot;module_id&quot;:&quot;car_style_module&quot;,&quot;car_series_id&quot;:&quot;1145&quot;,&quot;car_series_name&quot;:&quot;轩逸&quot;,&quot;car_style_id&quot;:&quot;58587&quot;,&quot;car_style_name&quot;:&quot;电驱版e-POWER 全电驱Pro&quot;}}\" " +
					"style=\"width: 74px; height: 32px;\"> " +
					"<a href=\"http://127.0.0.1:8899/html/car_purchase_calculation.html?" + car_series_id +
					"/" + item.car_model_id +
					"\" target=\"_blank\">购车计算</a> " +
					"</button> " +
					"</div> " +
					"</div> " +
					"</div> " +
					"</div> " +
					"</li>";
			});
			$("#all_car_model").append(all_car_model_data);
			if (data.all_car_model.length > 3) {
				$("#show_more_space").show(); //显示展开
				$("#all_car_model li:gt(2)").hide();
			}
			load_same_level_ranking("sales_volume"); /* 查询同级车排行 */
			if (user_id != 0) //已登录
				select_user_whether_follow_car_series(); /* 查询用户是否已关注该车系 */
		}, "json");
}

/* 查询同级车排行 */
function load_same_level_ranking(conditions_type) {
	$.post("http://127.0.0.1:8899/api/consumer/Car_seriesController/select_same_level_ranking/" + conditions_type +
		"/" + car_series_id + "/" + car_series_car_level_fk_id,
		function(data) {
			var span_text = "";
			var all_same_level_ranking_data = "";
			$(data).each(function(index, item) {
				if (index == 0) {
					span_text =
						"<span class=\"rank_rank-number__3fDBq\" style=\"background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/rank_s_1-8c04d288c29aabfd832f..png)\"></span>";
				} else if (index == 1) {
					span_text =
						"<span class=\"rank_rank-number__3fDBq\" style=\"background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/rank_s_2-cfed5d3c4cbd99aef324..png)\"></span>";
				} else if (index == 2) {
					span_text =
						"<span class=\"rank_rank-number__3fDBq\" style=\"background-image:url(//lf3-motor.dcarstatic.com/obj/motor-fe-static/motor/pc/_next/./static/images/rank_s_3-6700b1a36ec917c02f58..png)\"></span>";
				} else if (index == 3) {
					span_text = "<span class=\"rank_rank-number__3fDBq\">4</span>";
				} else {
					span_text = "<span class=\"rank_rank-number__3fDBq\">5</span>";
				}
				all_same_level_ranking_data +=
					"<div class=\"swiper-slide rank_rank-item__3DI_L\"><a href=\"http://127.0.0.1:8899/html/car_series_index.html?" +
					item.car_series_id +
					"\" target=\"_blank\" rel=\"opener\"> " +
					"<div class=\"rank_left__36ewy\">" + span_text +
					"<div class=\"rank_info-wrapper__881Aa\"> " +
					"<div style=\"display:inline-block;max-width:100%;overflow:hidden;position:relative;z-index:0\" " +
					"class=\"tw-flex-none\"> " +
					"<div " +
					"style=\"box-sizing:border-box;display:block;max-width:100%\"> " +
					"<img style=\"max-width:100%;display:block;margin:0;border:none;padding:0\" " +
					"src=\"data:image/svg+xml;utf-8,<svg width=&quot;100&quot; height=&quot;66&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; version=&quot;1.1&quot;/>\" " +
					"aria-hidden=\"true\" role=\"presentation\"></div> " +
					"<img src=\"http://120.48.47.144:8899/images/" + item.car_series_images_url_1 + "\" " +
					"alt=" + item.car_series_name + " width=\"100\" height=\"66\" " +
					"style=\"position:absolute;top:0;left:0;bottom:0;right:0;box-sizing:border-box;padding:0;border:none;margin:auto;display:block;width:0;height:0;min-width:100%;max-width:100%;min-height:100%;max-height:100%;object-fit:contain\" " +
					"class=\"zoom\"> " +
					"</div> " +
					"<div class=\"rank_info-main__UikJP\"> " +
					"<h1 class=\"rank_name__5KE9x g-one-line-text\">" + item.car_series_name + "</h1> " +
					"<p class=\"rank_price__th0vx g-one-line-text\"> " + item.min_manufacturer_price + "-" +
					item.max_manufacturer_price + "万</p> " +
					"</a></div> " +
					"</div> " +
					"</div> " +
					"<div class=\"rank_right__3NT6D\"> " +
					"<p class=\"rank_rank-count__12zBD\">" + (conditions_type == "sales_volume" ? item.sales
						.sales_count : (conditions_type == "popularity" ? item.car_series_popularity : item
							.car_points.car_points_avg)) + "</p>" +
					"</div></a> " +
					"</div>";
			});
			$(".swiper-wrapper div").remove();
			$(".swiper-wrapper").append(all_same_level_ranking_data);
		}, "json");
}

/* 查询用户是否已关注该车系 */
function select_user_whether_follow_car_series() {
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/Follow_car_seriesController/select_user_whether_follow_car_series",
		type: "post",
		data: JSON.stringify({
			"follow_car_series": {
				"follow_car_series_user_fk_id": user_id,
				"follow_car_series_fk_id": car_series_id
			}
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "json",
		success: function(data) {
			if (data.result == "true") {
				$("#follow").hide();
				$("#followed").show();
			}
		}
	});
}

/* 车型展开/收起 */
function more_space_car_series() {
	$("#all_car_model li:gt(2)").slideToggle();
	if ($("#show_more_space").css("display") == "none") {
		$("#show_more_space").show();
		$("#hide_more_space").hide();
	} else {
		$("#show_more_space").hide();
		$("#hide_more_space").show();
	}
}

/* 切换排行查询条件 */
function switch_ranking_conditions_type(conditions_type) {
	$(".rank_tags__Edels span").attr("class", "rank_tag__MiFIw");
	if (conditions_type == "sales_volume") {
		$(".rank_tags__Edels span:eq(0)").attr("class", "rank_tag__MiFIw rank_active__X18qk");
	} else if (conditions_type == "popularity") {
		$(".rank_tags__Edels span:eq(1)").attr("class", "rank_tag__MiFIw rank_active__X18qk");
	} else if (conditions_type == "car_points") {
		$(".rank_tags__Edels span:eq(2)").attr("class", "rank_tag__MiFIw rank_active__X18qk");
	}
	load_same_level_ranking(conditions_type);
}

/* 关注/取消关注 */
function insert_or_delete_follow_car_series() {
	if (user_id == 0) { //未登录
		location.href = "http://127.0.0.1:8899/html/car_login.html?car_series_index/" + car_series_id;
		return;
	}
	var type = $("#followed").css("display") == "none" ? "insert" : "delete";
	$.ajax({
		url: "http://127.0.0.1:8899/api/consumer/Follow_car_seriesController/insert_or_delete_follow_car_series",
		type: "post",
		data: JSON.stringify({
			"follow_car_series": {
				"follow_car_series_user_fk_id": user_id,
				"follow_car_series_fk_id": car_series_id
			},
			"type": type
		}),
		contentType: 'application/json;charset=utf-8',
		dataType: "json",
		success: function(data) {
			if (data.result == "true") {
				if (type == "insert") {
					$("#follow").hide();
					$("#followed").show();
				} else {
					$("#follow").show();
					$("#followed").hide();
				}
			}
		}
	});
}
